capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local data "";

/* 
Sample of SSI children 1990-2012, prep 4
*/

local exp "allssr";
local geo_list "cnty" ;

use "`crime'\prep2\crime_allssr_prepforreg.dta", clear;

foreach var of varlist n_crime* { ;
	gen N`var' = `var' ;
} ;

/* All */
preserve ;
collapse (mean) n_crime*
		 (count) N*
		 ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_all.dta", replace ;
restore ;

/* Par earn */
preserve ;
summ n_earnhhpre1217, det ;
replace n_earnhhpre1217 = 0 if n_earnhhpre1217 == . ;
gen byte parhigh = (n_earnhhpre1217 > r(p50) & n_earnhhpre1217 != .) ;
gen byte parlow = (n_earnhhpre1217 <= r(p50) & n_earnhhpre1217 != .) ;
replace parhigh = . if n_earnhhpre1217 == . ;
replace parlow = . if n_earnhhpre1217 == . ;
tab parhigh, m ;
tab parlow, m ;

collapse (mean) n_crime*
		 (count) N*
		 , by(parhigh parlow) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_parearn.dta", replace ;
restore ;

/* YOB */
preserve ;
collapse (mean) n_crime*
		 (count) N*
		 , by(dobyy) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_yob.dta", replace ;
restore ;

/* Diagnosis (dibdig) */
preserve ;
rename dibdig dibdig_char ;
destring dibdig_char, gen(dibdig) ;
gen byte diag_idment  = (dibdig >= 3170 & dibdig < 3200) ;
gen byte diag_othment = (dibdig >= 2900 & dibdig < 3200 & diag_idment == 0) ;
gen byte diag_nonment = (dibdig < 2900 | dibdig >= 3200) ;
gen diag 	 = "othment" if diag_othment == 1 ;
replace diag = "idment"  if diag_idment == 1 ;
replace diag = "nonment" if diag_nonment == 1 ;

collapse (mean) n_crime*
		 (count) N*
		 , by(diag) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_diag.dta", replace ;
restore ;

/* Family structure (toa) */
preserve ;
collapse (mean) n_crime*
		 (count) N*
		 , by(max_fam_singno) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_singno.dta", replace ;
restore ;

/* Race */
preserve ;
replace max_race_white = 0 if max_race_white == 1 & (max_race_black == 1 | max_race_nonw == 1) ;
collapse (mean) n_crime*
		 (count) N*
		 , by(max_race_white) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_white.dta", replace ;
restore ;

preserve ;
replace max_race_white = 0 if max_race_white == 1 & (max_race_black == 1 | max_race_nonw == 1) ;
collapse (mean) n_crime*
		 (count) N*
		 , by(max_race_black) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_black.dta", replace ;
restore ;

preserve ;
replace max_race_white = 0 if max_race_white == 1 & (max_race_black == 1 | max_race_nonw == 1) ;
collapse (mean) n_crime*
		 (count) N*
		 , by(max_race_nonw) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_nonw.dta", replace ;
restore ;


/* Sex */
preserve ;
drop if sex=="U" ;
collapse (mean) n_crime*
		 (count) N*
		 , by(sex) ;
foreach var of varlist 
	n_crime* 
	{;
	replace `var' = . if N`var'<10 ;
	} ;
save "`output'\\crime_allssr_summstats_sex.dta", replace ;
restore ;

capture log close;

